package itheima02;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author xxx
 * @date 2020/12/26 18:45
 * @description
 */
public class Test2 {
    public static void main(String[] args) throws IOException {
        //创建服务器端对象，指定输入服务器对应端口号,等待连接
        ServerSocket serverSocket=new ServerSocket(5555);

        //调用accpet方法连接到客户端对象
        Socket socket=serverSocket.accept();
        //读取客户端发送过来的消息
        InputStream inputStream=socket.getInputStream();

        int b=0;
        byte[] bytes=new byte[1024];

         b=inputStream.read(bytes);

        System.out.println(new String(bytes,0,b));

        OutputStream outputStream=socket.getOutputStream();
        outputStream.write("123456789".getBytes());

        serverSocket.close();
        socket.close();
        inputStream.close();

    }
}


/*
public class Test2  {
    public static void main(String[] args) throws IOException {
        System.out.println("服务端启动 , 等待连接 .... ");
    // 1.创建 ServerSocket对象，绑定端口，开始等待连接
        ServerSocket ss = new ServerSocket(6666);
    // 2.接收连接 accept 方法, 返回 socket 对象.
        Socket server = ss.accept();
    // 3.通过socket 获取输入流
        InputStream is = server.getInputStream();
    // 4.一次性读取数据 // 4.1 创建字节数组
        byte[] b = new byte[1024];
    // 4.2 据读取到字节数组中.
        int len = is.read(b);
    // 4.3 解析数组,打印字符串信息
        String msg = new String(b, 0, len);
        System.out.println(msg);
    //5.关闭资源.
        is.close();
        server.close();
    }
}*/
